package com.xht.controller;

import com.xht.constants.SystemConstants;
import com.xht.domain.ResponseResult;
import com.xht.domain.dto.AddCommentDto;
import com.xht.domain.entity.Comment;
import com.xht.enums.AppHttpCodeEnum;
import com.xht.exception.SystemExceptin;
import com.xht.service.CommentService;
import com.xht.utils.BeanCopyUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comment")
@Api(tags = "评论", description = "评论相关接口")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/commentList")
    public ResponseResult commentList(Long articleId, Integer pageNum, Integer pageSize) {
        return commentService.commentList(SystemConstants.COMMENT_TYPE_ARTICLE, articleId, pageNum, pageSize);
    }

    /**
     * 添加文章 评论
     * 注解RequestBody 告知 SpringMVC 请求参数 在 响应体 中获取
     */
    @PostMapping
    public ResponseResult addComment(@RequestBody AddCommentDto commentDto) {
        /**
         * 需要登录才能 评论
         */
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if ("anonymousUser".equals(authentication.getPrincipal()) || !authentication.isAuthenticated()) {
            throw new SystemExceptin(AppHttpCodeEnum.NEED_LOGIN);
        }
        Comment comment = BeanCopyUtils.copyBean(commentDto, Comment.class);
        return commentService.addComment(comment);
    }

    /**
     * 查询 友联评论
     */
    @GetMapping("/linkCommentList")
    public ResponseResult linkCommentList(Integer pageNum, Integer pageSize) {
        return commentService.commentList(SystemConstants.COMMENT_TYPE_LINK, null, pageNum, pageSize);
    }
}
